package com.chmingx.JavaBasic.thread.threaLocal;

/**
 * 通过synchronized加锁，进行资源隔离
 */
public class SynchronizedDemo {
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public static void main(String[] args) {
        SynchronizedDemo demo = new SynchronizedDemo();

        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                synchronized (demo) {
                    demo.setContent(Thread.currentThread().getName() + "的数据");
                    System.out.println("-------------------");
                    System.out.println(Thread.currentThread().getName() + "---->" + demo.getContent());
                }
            }, "线程"+ i).start();
        }
    }
}
